package com.lingxu.module.info.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.system.manage.vo.LoginUser;
import com.lingxu.module.info.entity.*;
import com.lingxu.module.info.mapper.InfoReleaseMapper;
import com.lingxu.module.info.service.IInfoReleaseService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 信息发布
 * @Author: 李德宁
 * @Date:   2023-06-14
 * @Version: V1.0.1
 */
@Service
public class InfoReleaseServiceImpl extends ServiceImpl<InfoReleaseMapper, InfoRelease> implements IInfoReleaseService {

     @Resource
     private InfoReleaseMapper infoReleaseMapper;

     private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 信息发布-分页列表查询
     * @param page 分页信息
     * @param infoRelease 信息发布
     * @return 信息发布列表
     */
    @Override
    public IPage<InfoRelease> queryPageList(Page<InfoRelease> page, InfoRelease infoRelease){
        return infoReleaseMapper.queryList(page,infoRelease);
    }

    /**
     * 信息发布-列表查询
     * @param infoRelease 信息发布
     * @return 信息发布列表
     */
    @Override
    public List<InfoRelease> queryList(InfoRelease infoRelease){
        return infoReleaseMapper.queryList(infoRelease);
    }

    @Override
    public IPage<InfoRelease> getInfoReleasePageList(Page<InfoRelease> page, InfoReleaseSearchVo infoRelease) {
        return infoReleaseMapper.getInfoReleasePageList(page,infoRelease);
    }

    @Override
    public List<InfoReleaseArea> getInfoReleaseArea(String region) {
        return infoReleaseMapper.getInfoReleaseArea(region);
    }

    @Override
    public List<ColumnInfoRelease> getColumnInfoRelease() {
        List<ColumnInfoRelease> list = new ArrayList<>();
        LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String region= user.getRegion();
        if(StringUtils.isBlank(region)){
            region="320000";
        }
        List<DictInfo> dictInfos = infoReleaseMapper.getDictInfos("columnType");
        for (DictInfo dictInfo:dictInfos){
            ColumnInfoRelease columnInfoRelease=new ColumnInfoRelease();
            columnInfoRelease.setDescription(dictInfo.getDescription());
            if("图文栏目".equals(dictInfo.getDescription())){
                columnInfoRelease.setIsshowphoto("1");
            }else {
                columnInfoRelease.setIsshowphoto("0");
            }
            columnInfoRelease.setColumnname(dictInfo.getName());
            columnInfoRelease.setColumntype(dictInfo.getValue());
            List<InfoRelease> infoReleases=infoReleaseMapper.getColumnInfoRelease(region, dictInfo.getValue());
            columnInfoRelease.setList(infoReleases);
            list.add(columnInfoRelease);
        }
        return list;
    }


    /**
     * @Author ysj闫世杰
     * @Description 获取发布主页的信息公告
     * @Date 2023/7/27 15:36
     * @Param:
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result findHomeIndexInfos() {
        try {
            List<InfoRelease> data = infoReleaseMapper.findHomeIndexInfos();
            return Result.ok(data,"查询成功");
        }catch (Exception e){
            logger.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }
}
